import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/base/controller/base_list_controller.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/user/model/model_user_wallet.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/date_picker/custom_date_picker_sheet.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/utils/permisson/permission_utils.dart';

// import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';

class DepositLogic extends BaseListController {
  Rx<ModelUserWallet> userWallet = Rx(ModelUserWallet());

  @override
  Future<List> loadData() async {
    await AppUserManager().loadWalletData();
    if (AppUserManager().walletList.isNotEmpty &&
        userWallet.value.symbol == null) {
      userWallet.value = AppUserManager().walletList.firstWhereOrNull(
              (item) => item.symbol?.toUpperCase() == "RTX") ??
          AppUserManager().walletList.first;
    }

    return [1];
    // TODO: implement loadData
    // return super.loadData();
  }

  @override
  void onInit() {
    super.onInit();
    // 初始化存款页数据
    // _loadDepositData();
  }

  @override
  void onReady() {
    super.onReady();
    // 页面准备就绪时的操作
  }

  @override
  void onClose() {
    super.onClose();
    // 页面关闭时的清理操作
  }

  // /// 加载充值数据
  // void _loadDepositData() {
  //   // TODO: 从API加载实际数据
  //   // 这里使用模拟数据
  // }
  //
  // /// 切换币种
  // void switchCurrency(String currency) {
  //   selectedCurrency.value = currency.toLowerCase();
  // }

  // /// 获取当前余额
  // String get currentBalance {
  //   return selectedCurrency.value == 'usdt'
  //       ? usdtBalance.value
  //       : trxBalance.value;
  // }
  //
  // /// 获取当前地址
  // String get currentAddress {
  //   return selectedCurrency.value == 'usdt'
  //       ? usdtAddress.value
  //       : trxAddress.value;
  // }
  //
  // /// 获取当前币种名称
  // String get currentCurrencyName {
  //   return selectedCurrency.value.toUpperCase();
  // }

  /// 复制地址
  void copyAddress() {
    Clipboard.setData(ClipboardData(
        text: AppUserManager().user.value?.rechargeAddress ?? ''));
    LoadingUtils.showToast(LocaleKeys.copySuccess.tr);
  }

  // /// 保存二维码到相册
  // Future<void> saveQrCodeToGallery(GlobalKey repaintBoundaryKey) async {
  //   try {
  //     // 检查存储权限
  //     bool hasPermission = await PermissionUtils.checkPermission(Permission.photos);
  //     if (!hasPermission) {
  //       LoadingUtils.showToast('需要存储权限才能保存图片');
  //       return;
  //     }
  //
  //     LoadingUtils.showLoading(status: '正在生成二维码图片...');
  //
  //     // 获取RepaintBoundary的RenderRepaintBoundary
  //     RenderRepaintBoundary boundary = repaintBoundaryKey.currentContext!
  //         .findRenderObject() as RenderRepaintBoundary;
  //
  //     // 将Widget转换为图片
  //     ui.Image image = await boundary.toImage(pixelRatio: 3.0);
  //     ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  //     Uint8List pngBytes = byteData!.buffer.asUint8List();
  //
  //     // 保存到相册
  //     final result = await ImageGallerySaver.saveImage(
  //       pngBytes,
  //       quality: 100,
  //       name: 'QRCode_${DateTime.now().millisecondsSinceEpoch}',
  //     );
  //
  //     LoadingUtils.dismiss();
  //
  //     if (result['isSuccess'] == true) {
  //       LoadingUtils.showToast('二维码已保存到相册');
  //     } else {
  //       LoadingUtils.showToast('保存失败，请重试');
  //     }
  //   } catch (e) {
  //     LoadingUtils.dismiss();
  //     LoadingUtils.showToast('保存失败：$e');
  //   }
  // }

  /// 打开存款教程
  void openDepositTutorial() {
    // TODO: 实现存款教程功能
    LoadingUtils.showToast(LocaleKeys.comingSoon.tr);
  }
}
